package com.hmall;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.sql.Connection;

@MapperScan("com.hmall.mapper")
@SpringBootApplication
public class HMallApplication {
    public static void main(String[] args) {
        SpringApplication.run(HMallApplication.class, args);
    }
    
    @Bean
    public CommandLineRunner testConnection(DataSource dataSource) {
        return args -> {
            System.out.println("测试数据库连接...");
            try (Connection conn = dataSource.getConnection()) {
                System.out.println("数据库连接成功! URL: " + conn.getMetaData().getURL());
            } catch (Exception e) {
                System.err.println("数据库连接失败!");
                e.printStackTrace();
            }
        };
    }
}